﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;



public partial class sysadmin_SystemInfo_TeacherClassLoginNum : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["OrderList"] = "";  //
            ViewState["OrderType"] = " desc ";//排序方式
            string search = GetSearch();
            //Response.Write(search);
            //Response.End();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_TeacherClassLoginNum_InfoTitle.Text = GetLanageValueByKey("lbl_TeacherClassLoginNum_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lbl_TeacherClassLoginNum_ClassName.Text = GetLanageValueByKey("lbl_TeacherClassLoginNum_ClassName", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append("SELECT distinct TTrainClass.TrainclaID as TrainclaID,(select sum(TUser.UserTotalLoginNum) from Tuser where TUser.UserID in (select FKTrainclastuUserID from TTrainClassStudent where  TTrainClassStudent.FKTrainclastuTrainclaID=TTrainClass.TrainclaID)) as TotalLoginNum, TTrainClass.TrainclaName as classname,TTrainClass.TrainclTimeType as timetype,TTrainClass.TrainclaStartTime as  StartTime,TTrainClass.TrainclaEndTime as EndTime, TTrainClass.TrainclTimeLength as TrainclTimeLength,TTrainClass.TrainclaStudentNum as StudentNum,TTrainClass.TrainclaSatate as Satate FROM TTrainClass INNER JOIN TTrainClassStudent ON  TTrainClass.TrainclaID = TTrainClassStudent.FKTrainclastuTrainclaID  where TTrainClass.TrainclUserType=2 and");

        if (!string.IsNullOrEmpty(txt_ClassName.Text))
        {
            sbSearch.Append(string.Format(" classname like '%{0}%' and ", txt_ClassName.Text));
        }

        sbSearch.Append(" 1=1 ");
        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = obj.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
        PreAspNetPager(AspNetPager1);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = new DataTable();

        string orderlist = ViewState["OrderList"].ToString();
        if (!string.IsNullOrEmpty(orderlist))
            search += " order by " + orderlist + ViewState["OrderType"].ToString();
        else
            search += " order by TrainclaID desc";

        //string orderList = " OnlineuserLoginTime desc ";

        //Response.Write(search);
        //Response.End();

        dt = obj.Query(search, LMSPageSize, pageIndex);

        dt.Columns.Add("strSatate");
        dt.Columns.Add("strtimetype");
        dt.Columns.Add("strStartTime");
        dt.Columns.Add("strEndTime");
        dt.Columns.Add("strTrainclTimeLength");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["strStartTime"] = dt.Rows[i]["StartTime"];
            dt.Rows[i]["strEndTime"] = dt.Rows[i]["EndTime"];
            dt.Rows[i]["strTrainclTimeLength"] = dt.Rows[i]["TrainclTimeLength"];
            if (!string.IsNullOrEmpty(dt.Rows[i]["Satate"].ToString()))
            {
                if (dt.Rows[i]["Satate"].ToString() == "0")
                {
                    dt.Rows[i]["strSatate"] = GetLanageValueByKey("State_Msg1_yishenhe", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
                else if (dt.Rows[i]["Satate"].ToString() == "1")
                {
                    dt.Rows[i]["strSatate"] = GetLanageValueByKey("State_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
            }
            if (!string.IsNullOrEmpty(dt.Rows[i]["timetype"].ToString()))
            {
                if (dt.Rows[i]["timetype"].ToString() == "1")
                {
                    dt.Rows[i]["strtimetype"] = GetLanageValueByKey("AddClass_db_d4", PowerCreator.LMS.Language.LanagetItemType.Page);
                    dt.Rows[i]["strStartTime"] = "-";
                    dt.Rows[i]["strEndTime"] = "-";
                }
                else if (dt.Rows[i]["timetype"].ToString() == "2")
                {
                    dt.Rows[i]["strtimetype"] = GetLanageValueByKey("AddClass_qishizhouqi", PowerCreator.LMS.Language.LanagetItemType.Page);
                    dt.Rows[i]["strTrainclTimeLength"] = "-";
                }
            }


        }
        
        rpt_TeacherClassLoginNum.DataSource = dt;
        rpt_TeacherClassLoginNum.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_CurrentSearch_Click(object sender, EventArgs e)
    {
        
    }

    protected void rpt_TeacherClassLoginNum_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "orderby")
        {
            ViewState["OrderList"] = "TUser."+e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search, AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }

        else if (e.CommandName.ToLower() == "lshow")
        {
            string url = string.Format(WebUtils.WebRoot + "/sysadmin/SystemInfo/TeacherLoginNum.aspx?TrainclaID={0}", e.CommandArgument.ToString());
            //string url = string.Format("StudentLoginNum.aspx");
            //string url = string.Format("StudentLoginNum.aspx?TrainclaID={0}", e.CommandArgument.ToString());
            Script.RedirectURL(url);
        }

    }
}
